﻿var Url = "AjaxData/CustomOperating.aspx";
//自动义双击方法
function CustomDbClick() {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    window.parent.$("#tabs").tabs('close', '查看约场订单');
    var pagetitle = "查看约场订单";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=5&id=' + vid + '&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}
//自定义新增方法
var fnCustomAdd = function () {
}
//自定义修改方法
var fnCustomEdit = function () {  //允许修改所有信息
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        window.parent.$("#tabs").tabs('close', '约场订单修改');
        var pagetitle = "约场订单修改";
        parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=4&id=' + vid + '&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
        parent.NeedRefreshPage = this;
    }
}
//自定义删除方法
var fnCustomDeleteData = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        var SaveUrl = Url + "?r=" + Math.random() + "&action=delete&entity=CrmCustomerOrder&json=&type=0&id=" + vid;
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {
                        YAHOO.example.container.dialog3.hide();
                        alert("删除成功!");
                        RefreshData();
                        return;
                    } else {
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}


//新增订单(约场)
var fnAddOrderField = function () {
    window.parent.$("#tabs").tabs('close', '新增约场订单');
    var pagetitle = "新增约场订单";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=0&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}
//订单变更(约场)
var fnOrderModify = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        var SaveUrl = Url + "?r=" + Math.random() + "&type=0&action=check&entity=CrmCustomerCheckField&json=&id=" + vid;
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {  //新建状态
                        OrderModifyField1(vid);
                    } else if (str == "2") { //待交付|交付中
                        OrderModifyField2(vid);
                    } else if (str == "3") {
                        alert("您当前的订单处于 签核中 状态，不能变更!");
                        return;
                    } else if (str == "4") {
                        alert("您当前的订单处于 结案 状态，不能变更!");
                        return;
                    } else if (str == "5") {
                        alert("您当前的订单处于 报废 状态，不能变更!");
                        return;
                    } else if (str == "6") {
                        alert("您当前选中的订单不是约场订单,不能变更!");
                        return;
                    } else {
                        alert(str);
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}
var OrderModifyField1 = function (vid) {
    window.parent.$("#tabs").tabs('close', '约场订单变更');
    var pagetitle = "约场订单变更";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=1&id=' + vid + '&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}
var OrderModifyField2 = function (vid) { 
    window.parent.$("#tabs").tabs('close', '约场订单变更');
    var pagetitle = "约场订单变更";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=2&id=' + vid + '&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}
var OrderModifyField3 = function (vid) { //查看
    window.parent.$("#tabs").tabs('close', '约场订单变更');
    var pagetitle = "约场订单变更";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderField.aspx?flag=5&id=' + vid + '&entity=' + const_EditEntity + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}

//订单审核
var fnCheckOrder = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("您确定审核该订单吗?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=update&entity=CrmCustomerOrder&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("订单审核成功!");
                            RefreshData();
                        }
                        else {
                            alert(str);
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}


//格式化讲场类型
var formatHmType = function (elCell, oRecord, oColumn, sData) {
    if (sData == "A") {
        elCell.innerHTML = "上午";
    } else if (sData == "P") {
        elCell.innerHTML = "下午";
    } else if (sData == "N") {
        elCell.innerHTML = "晚上";
    } else {
        elCell.innerHTML = "";
    }
}
//格式化客户名称列
var formatCustomerName = function (elCell, oRecord, oColumn, sData) {
    var id = oRecord._oData["CustomerId"];
    var state = oRecord._oData["OrderState"];
    elCell.innerHTML = "<a href='#' onclick=\"OrderModifyFieldCustomer('" + id + "','" + state + "')\">" + sData + "</a>";
}
var OrderModifyFieldCustomer = function (vid, text) {
    var SaveUrl = Url + "?r=" + Math.random() + "&type=1&action=check&entity=CrmCustomerInfoID&json=" + vid;
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "0") {
                    CustomerPage(vid, text, 0); //客户信息还没流到后端
                } else {
                    CustomerPage(vid, text, 1);
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}
var CustomerPage = function (vid, text, type) {
    if (type == "0") {
        if (text == "待交付") {
            window.parent.$("#tabs").tabs('close', '修改客户信息');
            var pagetitle = "修改客户信息";
            parent.addTabs(pagetitle, '../Crm/Customer/AddCustomer.aspx?r=' + Math.random() + '&type=0&flag=2&entity=CrmCustomer&id=' + vid + '&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        } else {
            window.parent.$("#tabs").tabs('close', '查看客户信息');
            var pagetitle = "查看客户信息";
            parent.addTabs(pagetitle, '../Crm/Customer/AddCustomer.aspx?r=' + Math.random() + '&entity=CrmCustomer&id=' + vid + '&title=' + encodeURI(pagetitle));
            parent.NeedRefreshPage = this;
        }
    } else {
        window.parent.$("#tabs").tabs('close', '查看客户信息');
        var pagetitle = "查看客户信息";
        parent.addTabs(pagetitle, '../Crm/Customer/AddCustomerInfo.aspx?entity=CrmCustomerInfo&title=' + encodeURI(pagetitle) + '&id=' + vid);
        parent.NeedRefreshPage = this;
    }
}

//销售订单变更(M)
var fnOrderSaleModify = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData("CustomerId");
    if (vid != "") {
        var SaveUrl = Url + "?r=" + Math.random() + "&type=1&action=check&entity=CrmCustomerInfoCheckField&json=&id=" + vid;
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1" || str == "2") {
                        OrderSaleModifys(vid);
                    } else if (str == "7") {
                        alert("您当前选中的订单不是销售订单,不能变更!");
                        return;
                    } else if (str == "3") {
                        alert("您当前的订单处于 签核中 状态，不能变更!");
                        return;
                    } else if (str == "4") {
                        alert("您当前的订单处于 结案 状态，不能变更!");
                        return;
                    } else if (str == "5") {
                        alert("您当前的订单处于 报废 状态，不能变更!");
                        return;
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
}
var OrderSaleModifys = function (vid) {
    window.parent.$("#tabs").tabs('close', '销售订单变更单');
    var pagetitle = "销售订单变更单";
    parent.addTabs(pagetitle, '../Crm/Sale/AddOrderSale.aspx?id=' + vid + '&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}

if (typeof ($) == "undefined") { //屏蔽右键
    parent.unContextMenu(document);
}
if (typeof (YAHOO) == "undefined") {
    parent.unContextMenu(document);
}